﻿using System;
using System.Collections.Generic;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    public class BattleTurnState : BattleBaseState
    {
        #region Get
        public BattleTurnQueueController queueCtrl => this.mPlayCtrl.queueCtrl;
        private Unit mCurActionUnit => this.queueCtrl.current.unit;
        private FSMSystem<EBattleSkillState, int> mSkillFsm => this.mPlayCtrl.skillFsm;
        #endregion

        public BattleTurnState(BattlePlayController playController) : base(playController)
        {
        }

        public override EBattleState stateId => EBattleState.Turn;

        public override void OnInit()
        {
            this.queueCtrl.ClearSkill();
        }

        public override void OnEnter(EBattleState prevStateId)
        {
            this.mSkillFsm.Start(EBattleSkillState.None);

            var rCurTurn = this.mPlayCtrl.queueCtrl.current;
            var rActionUnit = rCurTurn.unit;
            this.mPlayCtrl.OnTurnStart();
            
            var rCustom = rActionUnit.GetComponent<CustomPropertyComponent>(ETComponentType.CustomPropertyComponent);
            //有蓄力中的待释放技能就释放
            if(rCustom.GetProperty<bool>(ECustomProperty.Charge))
            {
                var rChargeComp = rActionUnit.GetComponent<ChargeComponent>(ETComponentType.ChargeComponent);
                // 蓄力技能释放
                if (!rChargeComp.ChargeReady(rCurTurn))
                {
                    this.mPlayCtrl.TurnEnd();
                    return;
                }
            }
            
            this.mSkillFsm.ChangeState(EBattleSkillState.PermitCast);
        }

        public override void OnExit()
        {
            this.mSkillFsm.Stop();
        }

        public override void Dispose()
        {
        }
    }
}